﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using AmiEmuLib.AmigaConfiguration;
using AmiEmuLib.AmigaConfiguration.ChipsetConfig;
using TomanuExtensions;
using System.IO;
using AmiEmuLib.Host.HostSound;
using System.Diagnostics;

namespace AmiEmuLib.Configuration.AmigaAudioConfig
{
    public static class AmigaAudioBleps
    {
        public static ReadOnlyCollection<AmigaAudioBlep> List { get; private set; }

        /// <summary>
        /// Static constructor.
        /// </summary>
        static AmigaAudioBleps()
        {
            using (Stream stream = StreamExtensions.FromResource(typeof(AmigaAudioBleps),
                "AmigaAudioBleps.xml"))
            {
                XmlReaderExtensions.ReadXml(stream, (reader) =>
                {
                    reader.ReadStartElement(typeof(AmigaAudioBleps).Name);

                    var list = new List<AmigaAudioBlep>();

                    while (reader.IsStartElement())
                        list.Add(new AmigaAudioBlep(reader));

                    reader.ReadEndElement();

                    List = list.AsReadOnly();
                });
            }
        }

        public static AmigaAudioBlep Find(string a_name, TVMode a_tv_mode, HostAudioSamplingRate a_sampling_rate, 
            AmigaAudioBlepFilterType a_filter_type)
        {
            var list = from blep in List
                       where blep.Filter == a_filter_type
                       where blep.SamplingRate == a_sampling_rate
                       where blep.TVMode == a_tv_mode
                       where blep.Name == a_name
                       select blep;

            Debug.Assert(list.Count() == 1);

            return list.First();
        }
    }
}
